package cnpm02.j2ee.group3.logic.dao.impl;

import java.util.Date;
import java.util.Calendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;

import cnpm02.j2ee.group3.logic.dao.generic.AbstractEntityDaoImpl;
import cnpm02.j2ee.group3.model.City;
import cnpm02.j2ee.group3.model.Ticket;

/**
 * Dao implementation for {@link TicketDao}.
 * @author BINH
 * @version 1.0
 * */
@Stateless
@Name("ticketDao")
@AutoCreate
public class TicketDaoImpl extends AbstractEntityDaoImpl<Ticket> implements
		TicketDao {

	@SuppressWarnings("unchecked")
	public List<Ticket> selectAll() {
		Query query = getEntityManager().createQuery(SELECT_ALL_QUERY);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Ticket> selectWithinTicket(City from, City to, Date date) {
		Date date1 = date;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, 7);
		Date date2 = cal.getTime();
		Query query = getEntityManager().createQuery(SELECT_TICKET_BY_DATE_AND_CITY);
		query.setParameter("date1", date1);
		query.setParameter("date2", date2);
		query.setParameter("from", from);
		query.setParameter("to", to);	
		return query.getResultList();
	}
}
